﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
using Jungle.Code.Utils;
using Jungle.Code.Core;

using Jungle.Code.News;

namespace Jungle.Code.News.WebControls
{
    public class NewsList: WebControl, IPostBackEventHandler
    {

        private int OnPage = 30;
        private int CurrPage = 1;

        //------------------------------------------------------------------------------------------------------
        public void RaisePostBackEvent(string eventArgument)
        {
            int pos = eventArgument.IndexOf(',');
            int l = eventArgument.Length;
            string s1 = eventArgument.Substring(0, pos);
            string s2 = eventArgument.Substring(pos + 1, (l - (pos + 1)));

            if (s1 == "chP") // перелистывание страницы
            {
                CurrPage = Convert.ToInt32(s2);
            }

            if (s1 == "delNews") // удаление пользователя
            {
                News n = new News();
                n.IdNews = Convert.ToInt32(s2);
                n.Delete();
            }
            

        }
        //--------------------------------------------------------------------
        protected override void RenderContents(HtmlTextWriter writer)
        {
            #region ProductsListTable
            string cmd = "";

            writer.AddAttribute("width", "100%");
            writer.AddAttribute("height", "100%");


            writer.RenderBeginTag(HtmlTextWriterTag.Table);

            writer.AddAttribute("bgcolor", "#CCCCCC");
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.AddAttribute("width", "30px");
            writer.AddAttribute("class", "products_table_td");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute("class", "products_table_title");
            writer.RenderBeginTag(HtmlTextWriterTag.Font);
            writer.Write("ID");
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.AddAttribute("width", "150px");
            writer.AddAttribute("class", "products_table_td");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute("class", "products_table_title");
            writer.RenderBeginTag(HtmlTextWriterTag.Font);
            writer.Write("Заголовок");
            writer.RenderEndTag();
            writer.RenderEndTag();
                        
            writer.AddAttribute("width", "150px");
            writer.AddAttribute("class", "products_table_td");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute("class", "products_table_title");
            writer.RenderBeginTag(HtmlTextWriterTag.Font);
            writer.Write("Дата добалвения");
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.AddAttribute("width", "100px");
            writer.AddAttribute("class", "products_table_td");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute("class", "products_table_title");
            writer.RenderBeginTag(HtmlTextWriterTag.Font);
            writer.Write("Действие");
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.RenderEndTag();

            //
            News nL = new News();
            nL.GetAll();


            bool bgChange = true;

            int count = 0;
            for (int i = (OnPage * CurrPage) - OnPage; i <= (OnPage * CurrPage) - 1; i++)
            {
                if (i >= nL.NewsList.Count) break;

                News n = nL.NewsList[i];

                if (bgChange) { writer.AddAttribute("bgcolor", "#FFFFFF"); bgChange = false; } else { bgChange = true; }
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);

                writer.AddAttribute("align", "center");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.AddAttribute("class", "products_table_text");
                writer.RenderBeginTag(HtmlTextWriterTag.Font);
                writer.Write(n.IdNews.ToString());
                writer.RenderEndTag();
                writer.RenderEndTag();

                writer.AddAttribute("align", "center");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.AddAttribute("class", "products_table_text");
                writer.RenderBeginTag(HtmlTextWriterTag.Font);
                writer.Write(n.Title.Trim());
                writer.RenderEndTag();
                writer.RenderEndTag();

                writer.AddAttribute("align", "center");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.AddAttribute("class", "products_table_text");
                writer.RenderBeginTag(HtmlTextWriterTag.Font);
                writer.Write(n.PubDate.ToLongDateString());
                writer.RenderEndTag();
                writer.RenderEndTag();
                
                writer.AddAttribute("align", "center");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.AddAttribute("class", "products_table_text");
                writer.RenderBeginTag(HtmlTextWriterTag.Font);
                PrintBtnDel(writer, n.IdNews);
                PrintBtnEdit(writer, n.IdNews);
                
                writer.RenderEndTag();
                writer.RenderEndTag();

                writer.RenderEndTag();

                count++;
            }
            //
            writer.RenderEndTag();

            if (nL.NewsList.Count % OnPage == 0)
                RenderPageLister(writer, nL.NewsList.Count / OnPage);
            else
                RenderPageLister(writer, (nL.NewsList.Count / OnPage) + 1);
            #endregion

            base.RenderContents(writer);
        }
        //-----------------------------------------------------------------------------------------------
        public HtmlTextWriter PrintBtnDel(HtmlTextWriter writer, int nId)
        {
            ClientScriptManager cs = Page.ClientScript;
            PostBackOptions opt = new PostBackOptions(this);
            opt.ActionUrl = Context.Request.RawUrl;
            opt.Argument = "delNews," + nId.ToString();

            writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "if(confirm(\"Вы действительно хотите удалить?\"))" + cs.GetPostBackEventReference(opt));
            writer.AddAttribute("title", "Удалить");
            writer.AddAttribute("style", "cursor: pointer;");
            PrintImage(writer, "/Admin/img/btn_blue_delete.jpg");
            return writer;
        }
        //-----------------------------------------------------------------------------------------------
        public HtmlTextWriter PrintBtnEdit(HtmlTextWriter writer, int nId)
        {
            string linkStr = Context.Request.RawUrl;
            int t = linkStr.IndexOf("cmd");

            if (t != -1)
            {
                linkStr = linkStr.Substring(0, t + 4);
            }

            writer.AddAttribute(HtmlTextWriterAttribute.Href, linkStr + "edit&nId=" + nId.ToString());
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.AddAttribute("title", "Редактировать");
            writer.AddAttribute("style", "cursor: pointer;");
            PrintImage(writer, "/Admin/img/btn_blue_edit.png");
            writer.RenderEndTag();
            return writer;
        }
        //-----------------------------------------------------------------------------------------------
        public HtmlTextWriter PrintImage(HtmlTextWriter writer, string imgUrl)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Src, UrlHelper.GetSiteUrl() + imgUrl);
            writer.AddAttribute(HtmlTextWriterAttribute.Align, "absmiddle");
            writer.RenderBeginTag(HtmlTextWriterTag.Img);
            writer.RenderEndTag();
            return writer;
        }
        //-----------------------------------------------------------------------------------------------
        private HtmlTextWriter RenderPageLister(HtmlTextWriter writer, int count)
        {
            ClientScriptManager cs = Page.ClientScript;
            PostBackOptions opt = new PostBackOptions(this);
            opt.ActionUrl = Context.Request.RawUrl;

            writer.Write("<b>Страница: </b>");
            for (int i = 1; i <= count; i++)
            {
                if (i == CurrPage)
                    writer.Write("<b>" + i.ToString() + "</b>");
                else
                {
                    opt.Argument = "chP," + i.ToString();
                    writer.AddAttribute(HtmlTextWriterAttribute.Onclick, cs.GetPostBackEventReference(opt));
                    writer.AddAttribute("style", "cursor: pointer;");
                    writer.RenderBeginTag(HtmlTextWriterTag.Span);
                    writer.Write(i.ToString());
                    writer.RenderEndTag();
                }
                if (i < count) writer.Write(", ");
            }
            return writer;
        }

    }
}
